<template>
	<jk-dialog
		v-model="showDialog"
		title="人员-班组"
		width="800px"
		:show-content-loading="showLoading"
		append-to-body
		:show-close-btn="true"
		:show-cancel-btn="false"
		:show-confirm-btn="false"
		:confirm-btn-loading="confirmBtnLoading"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<el-form ref="ruleForm" label-width="110px">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="编码">
						<span class="read-only-2">{{ ruleForm.code }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="名称">
						<span class="read-only-2">{{ ruleForm.name }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="区域">
						<span class="read-only-2">{{ ruleForm.workshopName }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="状态">
						<div class="read-only-2">
							<jk-common-status :status="ruleForm.auditState"></jk-common-status>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
			<div ref="box2" class="margin-bottom-4 flex-between-center">
				<div>
					<el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="checkboxEmpDialogState = true">人员</el-button>
				</div>
				<div class="flex-end-center">
					<jk-select v-model="searchParams.postId" :option-list="postList" size="mini" class="search-bar-item-size2 item-gap" placeholder="岗位" @change="onSelectPostChange"></jk-select>
					<el-button size="mini" type="default">
						<span>共：</span>
						<span style="font-size: 13px;font-weight: 700;">{{ tableData.length || 0 }}</span>
						<span> 人</span>
					</el-button>
				</div>
			</div>
			<jk-table border :loading="tableLoading" :data="tableData" :height="dialogTableHeight - 20">
				<vxe-column title="操作" align="center" field="operation" :width="70">
					<template slot-scope="scope">
						<el-button icon="el-icon-remove" type="text" size="mini" plain @click="onDeleteBtn(scope.row)"></el-button>
					</template>
				</vxe-column>
				<vxe-column title="姓名" align="left" field="name" :width="120" />
				<vxe-column title="性别" align="center" field="gender" :width="70">
					<template slot-scope="scope">
						<span>{{ scope.row.gender ? '男' : '女' }}</span>
					</template>
				</vxe-column>
				<vxe-column title="部门" align="left" field="deptName" :min-width="100" />
				<vxe-column title="区域" align="left" field="workshopName" :min-width="100" />
				<vxe-column title="班组" align="left" field="groupName" :min-width="100" />
				<vxe-column title="岗位" align="left" field="postName" :width="120" />
			</jk-table>
		</el-form>
		<checkbox-emp-dialog
			:default-select-rows="tableData"
			check-key-name="id"
			:dialog-state="checkboxEmpDialogState"
			@on-visible-change="onVisibleChangeOfCheckboxEmpDialog"
			@on-confirm="onConfirmBtnOfCheckboxEmpDialog"
		></checkbox-emp-dialog>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { personnelGroupGet, personnelGroupUpdateEmp } from '../../../api/personnel/group';
    import checkboxEmpDialog from './checkbox-emp-dialog';
    import { getEmpPage, systemEmpClearGroup } from '../../../api/system/emp';
    import { systemPostPage } from '../../../api/system/post';
    import { tipsBar } from '../../../utils/jk/jk-tool';

    export default {
        name: 'SetEmpForGroup',
        components: {
            checkboxEmpDialog
        },
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                searchParams: {
                    postId: null,
                    groupId: null
                },
                postList: [],
                confirmBtnLoading: false,
                checkboxEmpDialogState: false,
                tableData: [],
                tableLoading: false,
                showDialog: false,
                showLoading: true,
                ruleForm: {
                    auditState: 1
                }
            };
        },
        methods: {
            onSelectPostChange() {
                this.getEmpOfGroupItemListRequest();
            },
            onDeleteBtn(row) {
                this.$confirm(`确认将"${row.name}"从"${this.ruleForm.name}"中移除?`, { type: 'warning' }).then(() => {
                    systemEmpClearGroup({}, { id: row.id }).then(res => {
                        if (res.data.status === 200) {
                            tipsBar(this, 'custom', 'warning', '移除成功!');
                            this.getEmpOfGroupItemListRequest();
                        }
                    });
                }).catch(() => {});
            },
            onVisibleChangeOfCheckboxEmpDialog(e) {
                this.checkboxEmpDialogState = e;
            },
            onConfirmBtnOfCheckboxEmpDialog(arr) {
                const params = {};
                params.id = this.ruleForm.id;
                params.empList = arr.map(x => {
                    return {
                        id: x.id,
                        name: x.name
                    };
                });
                this.tableLoading = true;
                personnelGroupUpdateEmp(params).then(res => {
                    if (res.data.status === 200) {
                        tipsBar(this, 'custom', 'success', '添加成功!');
                        this.getEmpOfGroupItemListRequest();
                    }
                });
                this.checkboxEmpDialogState = false;
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getEmpOfGroupItemListRequest() {
                this.tableLoading = true;
                return getEmpPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        const resData = res.data.res ? JSON.parse(JSON.stringify(res.data.res)) : [];
                        this.tableData = resData;
                    }
                });
            },
            getDetailRequest() {
                return personnelGroupGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            getPostListRequest() {
                return systemPostPage({ auditState: 1, pageSize: 100 }).then(res => {
                    if (res.data.status === 200) {
                        this.postList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getPostListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.$set(this.searchParams, 'groupId', this.ruleForm.id);
                if (this.ruleForm.id) await this.getEmpOfGroupItemListRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal && this.dataId) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
